package tq.com.tqteacher.model;

import com.donkingliang.imageselector.utils.StringUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.tqcom.net.INetCallback;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.propertyEnum.TQModify;
import tq.com.tqteacher.view.TQQNoManagerView;

public class TQTWDYPublishModel extends TQNewPublish {
    private ArrayList<TQQNoManagerView.TQRegionData> mRegionData;

    public void setRegionData(ArrayList<TQQNoManagerView.TQRegionData> pData){
        mRegionData = pData;
    }

    public ArrayList<TQQNoManagerView.TQRegionData> getRegionData(){
        return mRegionData;
    }

    public TQQNoManagerView.TQRegionData firstRegion(){
        if (mRegionData == null || mRegionData.size() == 0) return null;
        return mRegionData.get(0);
    }

    public void onPulishComplete(INetCallback pNetCallback){
        HashMap<String, Object> param = new HashMap<>();
        param.put("category", Integer.valueOf(getType()));
        param.put("title", getName());
        param.put("desc", getDesc());
        param.put("grade", Integer.valueOf(getGrade()));
        param.put("chapter", Integer.valueOf(getParentId()));

        if(mRegionData != null){
            StringBuffer lBuffer = new StringBuffer();

            for (TQQNoManagerView.TQRegionData ldata:
                 mRegionData) {
                if(lBuffer.length() > 0){
                    lBuffer.append(",");
                }

                lBuffer.append(String.format("%s-%s", ldata.start, ldata.end));
            }
            param.put("question", lBuffer.toString());
        }

        TQNetInterface.newPublish(param, pNetCallback);
    }

    public void onModifyBaseInfo(INetCallback pNetCallback){
        HashMap<TQModify.MaterialBaseInfo, Object> param = new HashMap<>();
        param.put(TQModify.MaterialBaseInfo.title, getName());
        param.put(TQModify.MaterialBaseInfo.desc, getDesc());
        param.put(TQModify.MaterialBaseInfo.grade, String.valueOf(getGrade()));

        if(mRegionData != null){
            StringBuffer lBuffer = new StringBuffer();

            for (TQQNoManagerView.TQRegionData ldata:
                    mRegionData) {
                if(lBuffer.length() > 0){
                    lBuffer.append(",");
                }

                lBuffer.append(String.format("%s-%s", ldata.start, ldata.end));
            }
            param.put(TQModify.MaterialBaseInfo.question, lBuffer.toString());
        }

        TQNetInterface.modifyMaterialInfo(getSeq_id(), param, pNetCallback);
    }

    public void onMoifyQuestion(INetCallback pNetCallback){
        HashMap<TQModify.MaterialBaseInfo, Object> param = new HashMap<>();

        if(mRegionData != null){
            StringBuffer lBuffer = new StringBuffer();

            for (TQQNoManagerView.TQRegionData ldata:
                    mRegionData) {
                if(lBuffer.length() > 0){
                    lBuffer.append(",");
                }

                lBuffer.append(String.format("%s-%s", ldata.start, ldata.end));
            }
            param.put(TQModify.MaterialBaseInfo.question, lBuffer.toString());
        }

        TQNetInterface.modifyMaterialInfo(getSeq_id(), param, pNetCallback);
    }

    public void fromJson(JSONObject pObject) {
        super.fromJson(pObject);

        if (pObject == null) return;

        String q = pObject.optString("question");
        String[] q1 = q.split(",");

        ArrayList<TQQNoManagerView.TQRegionData> lRegionData= new ArrayList<>();
        for (String s:
             q1) {
            String[] se = s.split("-");

            if(se.length >= 2){
                TQQNoManagerView.TQRegionData lData = new TQQNoManagerView.TQRegionData();
                lData.start = se[0];
                lData.end = se[1];

                lRegionData.add(lData);
            }
        }

        if(lRegionData.size() > 0){
            mRegionData = lRegionData;
        }
    }
}
